package com.swn.cm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.cm.service.RoleService;
import com.swn.common.constants.ICommonConstants;
import com.swn.common.util.JSONUtil;

/**
 * 角色管理的类
 * @author 董士明
 * @date 2018-01-11
 *
 */

@Controller
@RequestMapping("/role")
public class RoleController {
	private static Logger logger = Logger.getLogger("RoleController.class");
	@Autowired
	private RoleService roleService;
	
	/**
	 * 查询角色
	 * @return
	 */
	@RequestMapping(value = "/queryRoleList")
	@ResponseBody
	public Map<String, Object> queryRoleList(){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String,Object>> list = roleService.queryRoleList();
			map.put("data", list);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	}
	/**
	 *  查询角色被授权的菜单中的具体功能
	 * @return
	 */
	@RequestMapping(value = "/queryRoleMenuList")
	@ResponseBody
	public Map<String, Object> queryRoleMenuList(){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String,Object>> list = roleService.queryRoleMenuList();
			map.put("data", list);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	}
	/**
	 *  查询角色具体每个菜单对应的具体功能组装成list用于tree展示
	 * @return
	 */
	@RequestMapping(value = "/queryRoleAuthorizedList")
	@ResponseBody
	public Map<String, Object> queryRoleAuthorizedList(String roleID){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String,Object>> list = roleService.queryRoleAuthorizedList(roleID);
			map.put("data", list);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	}
	/**
	 * 修改角色对应的菜单的具体授权
	 * @param signupForm
	 * @return
	 */
	@RequestMapping(value = "/updateRoleAuthorizedList" , method = RequestMethod.POST)
	@ResponseBody
	public String updateRoleAuthorizedList(String checkNodesJson,HttpSession session){
		String result = "fail";
		try {
			Map<String,Object> paramMap=new HashMap<String,Object>();
			@SuppressWarnings("unchecked")
			boolean tag= roleService.updateRoleAuthorizedList(checkNodesJson,session);
			if(tag){result="success";}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	/**
	 *  修改用户对应的角色
	 * @param infoJsonStr   授权内容
	 * @param userID      需要授权的用户id
	 * @return
	 */
	@RequestMapping(value = "/updateUserRole" , method = RequestMethod.POST)
	@ResponseBody
	public String updateUserRole(String infoJsonStr,String userID){
		String result = "fail";
		try {
			@SuppressWarnings("unchecked")
			boolean tag= roleService.updateUserRole(infoJsonStr,userID);
			if(tag){result="success";}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	/**
	 * 添加，修改角色
	 * @param signupForm
	 * @return
	 */
	@RequestMapping(value = "/saveRole" , method = RequestMethod.POST)
	@ResponseBody
	public String saveRole(String roleJsonForm){
		String result = "fail";
		try {
			Map<String,Object> paramMap=new HashMap<String,Object>();
			@SuppressWarnings("unchecked")
			Map<String,Object> roleMap = JSONUtil.getMapFromJsonString(roleJsonForm);
			boolean tag= roleService.saveRole(roleMap);
			if(tag){result="success";}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 根据id查询role
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryRoleMapById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryRoleMapById(String id) {
		Map<String, Object> map=null;
		try {
			map = roleService.queryRoleMapById(id);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return map;
	}
	
	
	
	/**
	 * 删除角色
	 * 
	 * @param id
	 * @return String
	 */
	@RequestMapping(value = "/deleteRole")
	@ResponseBody
	public String deleteRole(String id) {
		String result = "fail";
		try {
			boolean tag =roleService.queryRoleByBind(id);
			if(!tag){
				tag = roleService.deleteRole(id);
				if (tag){
					result = "success";
				}
			}else{
				result="hasBind";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	/**
	 * 查询角色对应的某个菜单的具体功能授权
	 * @return
	 */
	@RequestMapping(value = "/queryFeaturesByRole", method = RequestMethod.POST)
	@ResponseBody
	public String queryFeaturesByRole(String menuPath,HttpSession session) {
		String restr=""; //返回没有授权的功能标识用逗号分隔
		try {
			Map<String, Object>userMap=(Map<String, Object>)session.getAttribute(ICommonConstants.LOGIN_USER);
			String features=userMap.get("features").toString(); //所有的具体功能授权用逗号分隔
			String menuFeatures = roleService.queryFeaturesByRole(menuPath,userMap.get("role_id").toString());
			if(!menuFeatures.equals("")){
				for(String str:features.split(",")){
					boolean ishas=false;
					for(String fstr:menuFeatures.split(",")){
						if(str.equals(fstr)){
							ishas=true; //有授权
						}
					}
					if(!ishas){  //此功能没有授权
						if(!restr.contains(str)){
							 if(restr.equals("")){
								 restr=str;
							 }else{
								 restr+=","+str; 
							 }
						}
					}
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return restr;
	}
}
